/*
 * MapView.java
 */
package mapapp;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.Waypoint;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.WaypointPainter;
import org.jdesktop.swingx.mapviewer.WaypointRenderer;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.Painter;
import sakila.entity.Statii;
import sakila.entity.Semafoare;
import sakila.util.HibernateUtil;
import search.Bidirectional;
import search.Breadth;
import search.Depth;
import search.Hill;
import search.LeastCost;

/**
 * The application's main frame.
 */

public final class MapView extends FrameView {

     private static String QUERY="from Statii";
     private static String QUERYS="from Semafoare";

     public int clickedNr;

    public MapView(SingleFrameApplication app) {
        super(app);

        initComponents();
        clickedNr=0;

       

        jButton1.setText("Afiseaza");
        jLabel2.setText("Plecare");
        jLabel3.setText("long:");
        jLabel4.setText("lat:");
        jLabel5.setText("Sosire");
        jLabel6.setText("long:");
        jLabel7.setText("lat:");
        jLabel8.setText("Plecare");
        jLabel9.setText("Sosire");
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(selectStatii()));
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(selectStatii()));
        jButton2.setText("Afiseaza");
        jButton3.setText("Afiseaza");
        jLabel10.setText("Plecare");
        jLabel11.setText("Sosire");
        jMenuItem1.setText("Reseteaza");
        jLabel12.setText("Introduceti coordonatele:");
        jLabel13.setText("Selectati staiile:");
        jLabel14.setText("Alegeti statiile:");
        jLabel15.setText("Traseul dumneavoastra: ");
        jTextField7.setBackground(Color.ORANGE);
        jTextField7.setForeground(Color.WHITE);
        Font font1 = new Font("Ravie", Font.BOLD, 34);
        jTextField7.setFont(font1);
        jTextField7.setText("Intelligent GPS");


        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        jXMapKit1.setCenterPosition(new GeoPosition(45.749444, 21.227222));
        jXMapKit1.setMiniMapVisible(false);
        jXMapKit1.setZoom(5);

        this.afisareStatii();
        this.aplicareEtichete();
        this.clickStatie();
        this.clickSemafor();

    }

    public org.jdesktop.swingx.JXMapKit getMapKit() {
        return this.jXMapKit1;
    }


  private List runQuery() {
      List resultList=null;
    resultList=executeHQLQuery(QUERY);
    return resultList;
}
  private List runQueryS() {
      List resultListS=null;
    resultListS=executeHQLQuery(QUERYS);
    return resultListS;
}

  private List executeHQLQuery(String hql) {
      List resultList = null,resultListS=null;
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();


        Query q = session.createQuery(hql), qs = session.createQuery(hql);
        resultList = q.list();
        resultListS= qs.list();

        session.getTransaction().commit();

    } catch (HibernateException he) {
        he.printStackTrace();

    }
    return resultList;
}
  
  /*private List executeHQLQueryS(String hqls) {
      List resultListS = null;
    try {
        Session sessionS = HibernateUtil.getSessionFactory().openSession();
        sessionS.beginTransaction();


        Query qs = sessionS.createQuery(hqls);
        resultListS = qs.list();

        sessionS.getTransaction().commit();

    } catch (HibernateException he) {
        he.printStackTrace();

    }
    return resultListS;
}*/


    public final void afisareStatii() {


      Set<Waypoint> waypoints = new HashSet<Waypoint>();

      List resultList=null;
      resultList=runQuery();
      double lat,log;
      for(Object o : resultList) {
        Statii statie = (Statii)o;
        lat=Double.parseDouble(statie.getLatitudine());
        log=Double.parseDouble(statie.getLongitudine());


          waypoints.add(new Waypoint(lat,log));
      }

       WaypointPainter painter = new WaypointPainter();

       painter.setWaypoints(waypoints);
       painter.setRenderer(new WaypointRenderer() {
       public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) {
          g.setColor(Color.BLACK);
          g.fillOval(-5, -5, 5, 5);
          return true;
        }
});


    jXMapKit1.getMainMap().setOverlayPainter(painter);

    }

  public final void afisareSemafoare() {


      Set<Waypoint> waypoints = new HashSet<Waypoint>();

      List resultList=null;
      resultList=runQueryS();
      double lat,log;
      for(Object s : resultList) {
        Semafoare semafor = (Semafoare)s;
        lat=Double.parseDouble(semafor.getLatitudine());
        log=Double.parseDouble(semafor.getLongitudine());


          waypoints.add(new Waypoint(lat,log));
      }

       WaypointPainter painter = new WaypointPainter();

       painter.setWaypoints(waypoints);
       painter.setRenderer(new WaypointRenderer() {
       public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) {
          g.setColor(Color.BLUE);
          g.fillOval(-5, -5, 5, 5);
          return true;
        }
});


    jXMapKit1.getMainMap().setOverlayPainter(painter);

    }

    public void aplicareEtichete() {
        List resultList=null,resultListS=null;
      resultList=runQuery();
      resultListS=runQueryS();
      //
      double lat,log/*,latS,logS*/;
      for(Object o : resultList) {
        Statii statie = (Statii)o;
        final JLabel hoverLabel = new JLabel(statie.getNume());
        lat=Double.parseDouble(statie.getLatitudine());
        log=Double.parseDouble(statie.getLongitudine());
        hoverLabel.setVisible(false);
        jXMapKit1.getMainMap().add(hoverLabel);
        final Punct pct = new Punct(lat,log);
        jXMapKit1.getMainMap().addMouseMotionListener(new MouseMotionListener() {
         public void mouseDragged(MouseEvent e) { }

         public void mouseMoved(MouseEvent e) {



         Coordonate cord = new Coordonate();
         Point converted_gp_pt = cord.convertToPixel(pct.latitude, pct.longitude);
         //check if near the mouse
         if (converted_gp_pt.distance(e.getPoint()) < 5) {
             hoverLabel.setLocation(converted_gp_pt);
             hoverLabel.setVisible(true);
            } else {
                     hoverLabel.setVisible(false);
                   }
                      }

         });
    }

       for(Object s : resultListS) {
        Semafoare semafor = (Semafoare)s;
        final JLabel hoverLabel = new JLabel(semafor.getNume());
        lat=Double.parseDouble(semafor.getLatitudine());
        log=Double.parseDouble(semafor.getLongitudine());
        hoverLabel.setVisible(false);
        jXMapKit1.getMainMap().add(hoverLabel);
        final Punct pct = new Punct(lat,log);
        jXMapKit1.getMainMap().addMouseMotionListener(new MouseMotionListener() {
         public void mouseDragged(MouseEvent e) { }

         public void mouseMoved(MouseEvent e) {



         Coordonate cord1 = new Coordonate();
         Point converted_gp_pt = cord1.convertToPixel(pct.latitude, pct.longitude);
         //check if near the mouse
         if (converted_gp_pt.distance(e.getPoint()) < 5) {
             hoverLabel.setLocation(converted_gp_pt);
             hoverLabel.setVisible(true);
            } else {
                     hoverLabel.setVisible(false);
                   }
                      }

         });
    }
    }

    public void clickStatie(){


         jXMapKit1.getMainMap().addMouseListener(new MouseInputAdapter() {


                    public void mouseClicked(MouseEvent e) {

                              clickedNr++;
                              System.out.println("Double clicked " + clickedNr);

                        try{
                            statement = connection.getConnection().prepareStatement("SELECT * FROM `statii`");
                            ResultSet result = statement.executeQuery();


                        while(result.next()){
                          
                        
                        Point pt = e.getPoint();

                        // Get the pixel coordinates of the waypoint in question from the map.

                        JXMapViewer map = jXMapKit1.getMainMap();

                        Point2D point = map.getTileFactory().geoToPixel(new GeoPosition(Double.parseDouble(result.getString(3)), Double.parseDouble(result.getString(4))), map.getZoom());

// Adjust the pixel coordinates to their relative position on screen.

                        Rectangle bounds = map.getViewportBounds();
                        int x = (int) (point.getX() - bounds.getX());
                        int y = (int) (point.getY() - bounds.getY());

// Create a bounding rectangle around the waypoint, and see if the mouse click occured
// within its boundaries.

                        Rectangle rect = new Rectangle(x - 5, y - 5, 10, 10);

                        if (rect.contains(pt)) {

                                     
                                     

                                     if(clickedNr%2==1){

                                   
                                             jTextField5.setText(result.getString(2));
                                         

                                     }  else{

                                       
                                             jTextField6.setText(result.getString(2));
                                         }
                            }
                            }
                        } catch (SQLException exc) {
                                     System.out.println(exc.getMessage());
                                 }
                         
                        

             }
                });



    }
    public String[] selectStatii() {
        String[] statii = null;
        try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` ");
            statement = connection.getConnection().prepareStatement("SELECT * FROM `strazi` ");
            final ResultSet result = statement.executeQuery();

            result.last();
            statii = new String[result.getRow() - 1];
            result.first();
            int i = 0;
            while (result.next()) {
                statii[i++] = result.getString(2);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return statii;
    }

     public void clickSemafor(){


         jXMapKit1.getMainMap().addMouseListener(new MouseInputAdapter() {


                    public void mouseClicked(MouseEvent e) {

                              clickedNr++;
                              System.out.println("Double clicked " + clickedNr);

                        try{
                            statement = connection.getConnection().prepareStatement("SELECT * FROM `semafoare`");
                            ResultSet result = statement.executeQuery();


                        while(result.next()){


                        Point pt = e.getPoint();

                        // Get the pixel coordinates of the waypoint in question from the map.

                        JXMapViewer map = jXMapKit1.getMainMap();

                        Point2D point = map.getTileFactory().geoToPixel(new GeoPosition(Double.parseDouble(result.getString(3)), Double.parseDouble(result.getString(4))), map.getZoom());

// Adjust the pixel coordinates to their relative position on screen.

                        Rectangle bounds = map.getViewportBounds();
                        int x = (int) (point.getX() - bounds.getX());
                        int y = (int) (point.getY() - bounds.getY());

// Create a bounding rectangle around the waypoint, and see if the mouse click occured
// within its boundaries.

                        Rectangle rect = new Rectangle(x - 5, y - 5, 10, 10);

                        if (rect.contains(pt)) {




                                     if(clickedNr%2==1){


                                             jTextField5.setText(result.getString(2));


                                     }  else{


                                             jTextField6.setText(result.getString(2));
                                         }
                            }
                            }
                        } catch (SQLException exc) {
                                     System.out.println(exc.getMessage());
                                 }



             }
                });



    }
    public String[] selectSemafoare() {
        String[] semafoare = null;
        try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `semafoare` ");
            statement = connection.getConnection().prepareStatement("SELECT * FROM `strazi` ");
            final ResultSet result = statement.executeQuery();

            result.last();
            semafoare = new String[result.getRow() - 1];
            result.first();
            int i = 0;
            while (result.next()) {
                semafoare[i++] = result.getString(2);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return semafoare;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jXMapKit1 = new org.jdesktop.swingx.JXMapKit();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel15 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();

        mainPanel.setName("mainPanel"); // NOI18N

        jXMapKit1.setDefaultProvider(org.jdesktop.swingx.JXMapKit.DefaultProviders.OpenStreetMaps);
        jXMapKit1.setMiniMapVisible(false);
        jXMapKit1.setAddressLocationShown(false);
        jXMapKit1.setName("jXMapKit1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mapapp.MapApp.class).getContext().getResourceMap(MapView.class);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(mapapp.MapApp.class).getContext().getActionMap(MapView.class, this);
        jButton1.setAction(actionMap.get("afisare")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.setName("jComboBox2"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jButton2.setAction(actionMap.get("afisareCb")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jTextField5.setEditable(false);
        jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
        jTextField5.setName("jTextField5"); // NOI18N

        jTextField6.setEditable(false);
        jTextField6.setText(resourceMap.getString("jTextField6.text")); // NOI18N
        jTextField6.setName("jTextField6"); // NOI18N

        jButton3.setAction(actionMap.get("afiseazaClick")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
        jLabel15.setName("jLabel15"); // NOI18N

        jTextField7.setEditable(false);
        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                    .addComponent(jLabel15)
                    .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel15)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXMapKit1, javax.swing.GroupLayout.PREFERRED_SIZE, 805, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel12)
                                .addGroup(mainPanelLayout.createSequentialGroup()
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel4))
                                    .addGap(31, 31, 31)
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 6, Short.MAX_VALUE)
                                        .addComponent(jTextField1)))
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(mainPanelLayout.createSequentialGroup()
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel7))
                                    .addGap(32, 32, 32)
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField2))))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(jButton1)))
                        .addGap(63, 63, 63)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel11)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel10)
                            .addComponent(jLabel5)
                            .addComponent(jLabel14)
                            .addComponent(jButton3)
                            .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                            .addComponent(jTextField6)))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXMapKit1, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14))
                        .addGap(15, 15, 15)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(21, 21, 21)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel11)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1)
                                .addComponent(jButton2)
                                .addComponent(jButton3)))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setAction(actionMap.get("reseteaza")); // NOI18N
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        jTextArea1.setText("");
        //this.afisareStatii();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    @Action
    public void afisare() {
        //create a Set of waypoints
        final double longitudeP = Double.parseDouble(jTextField1.getText());
        final double latitudeP = Double.parseDouble(jTextField3.getText());
        final double longitudeS = Double.parseDouble(jTextField2.getText());
        final double latitudeS = Double.parseDouble(jTextField4.getText());

        String IDto=null, IDfrom=null;
        Depth ob = new Depth();

        java.sql.PreparedStatement statement1=null;
        java.sql.PreparedStatement statement2=null;
        ResultSet result1=null;
        ResultSet result2=null;
        int indice=0;
        String mijTr="";
        int IDstatie1=0;
        int IDstatie2=0;
        String idTraseu="";
        int index=0;



    try{
              statement1 = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE `latitudine` LIKE "+latitudeP+" AND `longitudine` LIKE "+longitudeP+"");
              statement2 = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE `latitudine` LIKE "+latitudeS+" AND `longitudine` LIKE "+longitudeS+"");
              result1 = statement1.executeQuery();
              result2 = statement2.executeQuery();
              while (result1.next()) {
              IDfrom=result1.getString(1);

            }
              while (result2.next()) {
              IDto=result2.getString(1);

            }


        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        
         

        ArrayList<String> statii=new ArrayList<String>();
        Punct pct=null;
        final ArrayList<Punct> waypts=new ArrayList<Punct>();
        double longt=0,lat=0;
        ob.setup();

        for(int i=0;i<statii.size();i++){
            System.out.println(statii.get(i));
        }
         ob.isRoute(IDfrom, IDto);
         if(ob.btStack.size() != 0)
         statii=ob.route(IDto);

         String string=" De la ";
          for(int i=0;i<statii.size();i++){

             try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i) + "'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
                longt = result.getDouble(3);
                lat = result.getFloat(4);
                pct=new Punct(longt,lat);
                waypts.add(pct);


            }

             }catch(Exception e){
                //ignore
            }
              try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i) + "'");
            ResultSet result = statement.executeQuery();
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i+1) + "'");
             result1=statement.executeQuery();
           
            while (result.next()) {
               IDstatie1=result.getInt(1);
            }
            while (result1.next()){
                IDstatie2=result1.getInt(1);
            }

             }catch(Exception e){
                //ignore
            }
             System.out.println(IDstatie1+" "+IDstatie2);
               try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `trasee` WHERE `Id_statie` = '" + IDstatie1 + "'");
            ResultSet result = statement.executeQuery();

          
            while (result.next()) {
                 idTraseu=result.getString(1);
                 index=result.getInt(3)+1;
              
              
            }
             statement = connection.getConnection().prepareStatement("SELECT * FROM `trasee` WHERE `Id_statie` = '" + IDstatie2 + "' AND `Id_traseu` LIKE '"+idTraseu+"' AND `Index`='"+index+"'");
               result1=statement.executeQuery();
               while (result1.next()){
                   
              // mijTr=result1.getString(1);

            }

             }catch(Exception e){
                //ignore
            }
             ////////////////////////////////////////////////////////////////
               try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `grafstatii` WHERE `Id_statie1` = '" + IDstatie1 + "' AND `Id_statie2` = '"+IDstatie2+"'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
               indice=result.getInt(1);



            }

             }catch(Exception e){
                //ignore
            }
            string = string.concat(statii.get(i));
             mijTr=getMijTr(indice);
            string =string.concat(mijTr);
            string=string.concat("\n");
            if(i!=statii.size()-1)
            string = string.concat(" la statia ");

         }
         jTextArea1.setText(string);



        Set<Waypoint> waypoints = new HashSet<Waypoint>();

        for(int i=0;i<waypts.size();i++){
            waypoints.add(new Waypoint(waypts.get(i).latitude,waypts.get(i).longitude));
        }

        //crate a WaypointPainter to draw the points
        WaypointPainter painter = new WaypointPainter();
        painter.setWaypoints(waypoints);

        painter.setRenderer(new WaypointRenderer() {

            public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) {
                g.setColor(Color.BLACK);
                g.fillOval(-3, -3, 5, 5);
                return true;
            }
        });




           Painter<JXMapViewer> line = new Painter<JXMapViewer>() {


            public void paint(Graphics2D g, JXMapViewer map, int h, int w) {
                g = (Graphics2D) g.create();
                //convert from viewport to world bitmap
                Rectangle rect = map.getViewportBounds();
                g.translate(-rect.x, -rect.y);
                ArrayList<GeoPosition> pcts=new ArrayList<GeoPosition>();
                for (int i=0;i<waypts.size();i++){
                        pcts.add(new GeoPosition(waypts.get(i).latitude,waypts.get(i).longitude));
                 }
                ArrayList<Point2D> pcts2D=new ArrayList<Point2D>();
                for (int i=0;i<pcts.size();i++){
                       pcts2D.add(map.getTileFactory().geoToPixel(pcts.get(i), map.getZoom()));
                }

                g.setColor(Color.red);
                for (int i=0; i<pcts2D.size()-1; i++) {
                  g.drawLine((int)pcts2D.get(i).getX(), (int) pcts2D.get(i).getY(), (int) pcts2D.get(i+1).getX(), (int) pcts2D.get(i+1).getY());

                }
                g.dispose();


            }
        };


        CompoundPainter cp = new CompoundPainter();
        cp.setPainters(line, painter);
        cp.setCacheable(false);
        jXMapKit1.getMainMap().setOverlayPainter(cp);

        
    }

    @Action
    public void afisareCb() {

        
        String selected1 = (String) jComboBox1.getSelectedItem();
        String selected2 = (String) jComboBox2.getSelectedItem();
        double long1 = 0, long2 = 0, lat1 = 0, lat2 = 0;
        String IDto=null, IDfrom=null;
        Depth ob = new Depth();

        try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + selected1 + "'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
                long1 = result.getDouble(3);
                lat1 = result.getFloat(4);
                IDfrom=result.getString(1);
            }

            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + selected2 + "'");
            result = statement.executeQuery();

            while (result.next()) {
                long2 = result.getDouble(3);
                lat2 = result.getFloat(4);
                IDto=result.getString(1);
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        ArrayList<String> statii=new ArrayList<String>();
        Punct pct=null;
        final ArrayList<Punct> waypts=new ArrayList<Punct>();
        double longt=0,lat=0;
        ob.setup();


         ob.isRoute(IDfrom, IDto);
         if(ob.btStack.size() != 0)
        statii=ob.route(IDto);

         int IDstatie1=0;
         int IDstatie2=0;
         String string=" De la ";
         String mijTr="";
         int index=0;
         String idTraseu="";
         int indice=0;
        

         for(int i=0;i<statii.size();i++){

             try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i) + "'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
                longt = result.getDouble(3);
                lat = result.getFloat(4);
                pct=new Punct(longt,lat);
                
                waypts.add(pct);



            }

             }catch(Exception e){
                //ignore
            }
             
             /////////////////////////Nu merge din cauza bazei de date!!!
              try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i) + "'");
            ResultSet result = statement.executeQuery();
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i+1) + "'");
            ResultSet result1=statement.executeQuery();
           
            while (result.next()) {
               IDstatie1=result.getInt(1);
            }
            while (result1.next()){
                IDstatie2=result1.getInt(1);
            }

             }catch(Exception e){
                //ignore
            }
             System.out.println(IDstatie1+" "+IDstatie2);
               try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `trasee` WHERE `Id_statie` = '" + IDstatie1 + "'");
            ResultSet result = statement.executeQuery();

          
            while (result.next()) {
                 idTraseu=result.getString(1);
                 index=result.getInt(3)+1;
              
              
            }
             statement = connection.getConnection().prepareStatement("SELECT * FROM `trasee` WHERE `Id_statie` = '" + IDstatie2 + "' AND `Id_traseu` LIKE '"+idTraseu+"' AND `Index`='"+index+"'");
               ResultSet result1=statement.executeQuery();
               while (result1.next()){
                   
              // mijTr=result1.getString(1);

            }

             }catch(Exception e){
                //ignore
            }
             ////////////////////////////////////////////////////////////////
               try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `grafstatii` WHERE `Id_statie1` = '" + IDstatie1 + "' AND `Id_statie2` = '"+IDstatie2+"'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
               indice=result.getInt(1);



            }

             }catch(Exception e){
                //ignore
            }
            string = string.concat(statii.get(i));
            mijTr=getMijTr(indice);
            string =string.concat(mijTr);
            string=string.concat("\n");
            if(i!=statii.size()-1)
            string = string.concat(" la statia ");

         }
         jTextArea1.setText(string);

     

        Set<Waypoint> waypoints = new HashSet<Waypoint>();
       
        for(int i=0;i<waypts.size();i++){
            waypoints.add(new Waypoint(waypts.get(i).latitude,waypts.get(i).longitude));
        }

        //crate a WaypointPainter to draw the points
        WaypointPainter painter = new WaypointPainter();
        painter.setWaypoints(waypoints);

        painter.setRenderer(new WaypointRenderer() {

            public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) {
                g.setColor(Color.BLACK);
                g.fillOval(-3, -3, 5, 5);
                return true;
            }
        });

       

       
           Painter<JXMapViewer> line = new Painter<JXMapViewer>() {


            public void paint(Graphics2D g, JXMapViewer map, int h, int w) {
                g = (Graphics2D) g.create();
                //convert from viewport to world bitmap
                Rectangle rect = map.getViewportBounds();
                g.translate(-rect.x, -rect.y);
                ArrayList<GeoPosition> pcts=new ArrayList<GeoPosition>();
                for (int i=0;i<waypts.size();i++){
                        pcts.add(new GeoPosition(waypts.get(i).latitude,waypts.get(i).longitude));
                 }
                ArrayList<Point2D> pcts2D=new ArrayList<Point2D>();
                for (int i=0;i<pcts.size();i++){
                       pcts2D.add(map.getTileFactory().geoToPixel(pcts.get(i), map.getZoom()));
                }

                g.setColor(Color.red);
                for (int i=0; i<pcts2D.size()-1; i++) {
                  g.drawLine((int)pcts2D.get(i).getX(), (int) pcts2D.get(i).getY(), (int) pcts2D.get(i+1).getX(), (int) pcts2D.get(i+1).getY());

                }
                g.dispose();
                

            }
        };
    

        CompoundPainter cp = new CompoundPainter();
        cp.setPainters(line, painter);
        cp.setCacheable(false);
        jXMapKit1.getMainMap().setOverlayPainter(cp);

    
        
        
    }

    @Action
    public void afiseazaClick() {
         String selected1 = jTextField5.getText();
        String selected2 =  jTextField6.getText();
        double long1 = 0, long2 = 0, lat1 = 0, lat2 = 0;

        try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `statii` WHERE nume = '" + selected1 + "'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
                long1 = result.getDouble(3);
                lat1 = result.getFloat(4);
            }

            statement = connection.getConnection().prepareStatement("SELECT * FROM `statii` WHERE nume = '" + selected2 + "'");
            result = statement.executeQuery();

            while (result.next()) {
                long2 = result.getDouble(3);
                lat2 = result.getFloat(4);
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        final double longitudeP = long1;
        final double latitudeP = lat1;
        final double longitudeS = long2;
        final double latitudeS = lat2;
        int IDstatie1=0;
        int IDstatie2=0;
        String idTraseu="";
        int index=0;

        String IDto=null, IDfrom=null;
        Depth ob = new Depth();

        java.sql.PreparedStatement statement1=null;
        java.sql.PreparedStatement statement2=null;
        ResultSet result1=null;
        ResultSet result2=null;
        int indice=0;
        String mijTr="";



    try{
              statement1 = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE `latitudine` LIKE "+latitudeP+" AND `longitudine` LIKE "+longitudeP+"");
              statement2 = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE `latitudine` LIKE "+latitudeS+" AND `longitudine` LIKE "+longitudeS+"");
              result1 = statement1.executeQuery();
              result2 = statement2.executeQuery();
              while (result1.next()) {
              IDfrom=result1.getString(1);

            }
              while (result2.next()) {
              IDto=result2.getString(1);

            }


        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }



        ArrayList<String> statii=new ArrayList<String>();
        Punct pct=null;
        final ArrayList<Punct> waypts=new ArrayList<Punct>();
        double longt=0,lat=0;
        ob.setup();

        for(int i=0;i<statii.size();i++){
            System.out.println(statii.get(i));
        }
         ob.isRoute(IDfrom, IDto);
         if(ob.btStack.size() != 0)
         statii=ob.route(IDto);

         String string=" De la ";
          for(int i=0;i<statii.size();i++){

             try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i) + "'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
                longt = result.getDouble(3);
                lat = result.getFloat(4);
                pct=new Punct(longt,lat);
                waypts.add(pct);


            }

             }catch(Exception e){
                //ignore
            }
              try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i) + "'");
            ResultSet result = statement.executeQuery();
            statement = connection.getConnection().prepareStatement("SELECT * FROM `toatestatiile` WHERE nume = '" + statii.get(i+1) + "'");
            result1=statement.executeQuery();

            while (result.next()) {
               IDstatie1=result.getInt(1);
            }
            while (result1.next()){
                IDstatie2=result1.getInt(1);
            }

             }catch(Exception e){
                //ignore
            }
             System.out.println(IDstatie1+" "+IDstatie2);
               try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `trasee` WHERE `Id_statie` = '" + IDstatie1 + "'");
            ResultSet result = statement.executeQuery();


            while (result.next()) {
                 idTraseu=result.getString(1);
                 index=result.getInt(3)+1;


            }
             statement = connection.getConnection().prepareStatement("SELECT * FROM `trasee` WHERE `Id_statie` = '" + IDstatie2 + "' AND `Id_traseu` LIKE '"+idTraseu+"' AND `Index`='"+index+"'");
               result1=statement.executeQuery();
               while (result1.next()){

              // mijTr=result1.getString(1);

            }

             }catch(Exception e){
                //ignore
            }
             ////////////////////////////////////////////////////////////////
               try {
            statement = connection.getConnection().prepareStatement("SELECT * FROM `grafstatii` WHERE `Id_statie1` = '" + IDstatie1 + "' AND `Id_statie2` = '"+IDstatie2+"'");
            ResultSet result = statement.executeQuery();
            //create a Set of waypoints
            while (result.next()) {
               indice=result.getInt(1);



            }

             }catch(Exception e){
                //ignore
            }
            string = string.concat(statii.get(i));
             mijTr=getMijTr(indice);
            string =string.concat(mijTr);
            string=string.concat("\n");
            if(i!=statii.size()-1)
            string = string.concat(" la statia ");

         }
         jTextArea1.setText(string);



        Set<Waypoint> waypoints = new HashSet<Waypoint>();

        for(int i=0;i<waypts.size();i++){
            waypoints.add(new Waypoint(waypts.get(i).latitude,waypts.get(i).longitude));
        }

        //crate a WaypointPainter to draw the points
        WaypointPainter painter = new WaypointPainter();
        painter.setWaypoints(waypoints);

        painter.setRenderer(new WaypointRenderer() {

            public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) {
                g.setColor(Color.BLACK);
                g.fillOval(-3, -3, 5, 5);
                return true;
            }
        });




           Painter<JXMapViewer> line = new Painter<JXMapViewer>() {


            public void paint(Graphics2D g, JXMapViewer map, int h, int w) {
                g = (Graphics2D) g.create();
                //convert from viewport to world bitmap
                Rectangle rect = map.getViewportBounds();
                g.translate(-rect.x, -rect.y);
                ArrayList<GeoPosition> pcts=new ArrayList<GeoPosition>();
                for (int i=0;i<waypts.size();i++){
                        pcts.add(new GeoPosition(waypts.get(i).latitude,waypts.get(i).longitude));
                 }
                ArrayList<Point2D> pcts2D=new ArrayList<Point2D>();
                for (int i=0;i<pcts.size();i++){
                       pcts2D.add(map.getTileFactory().geoToPixel(pcts.get(i), map.getZoom()));
                }

                g.setColor(Color.red);
                for (int i=0; i<pcts2D.size()-1; i++) {
                  g.drawLine((int)pcts2D.get(i).getX(), (int) pcts2D.get(i).getY(), (int) pcts2D.get(i+1).getX(), (int) pcts2D.get(i+1).getY());

                }
                g.dispose();


            }
        };


        CompoundPainter cp = new CompoundPainter();
        cp.setPainters(line, painter);
        cp.setCacheable(false);
        jXMapKit1.getMainMap().setOverlayPainter(cp);
    }
String getMijTr(int indice){
    if(indice>=1 && indice <=30)
        return " (Tramvai 1) ";
    else if(indice>=31 && indice <=66)
        return " (Tramvai 2) ";
    else if(indice>=67 && indice <=95)
        return " (Tramvai 3) ";
    else if(indice>=96 && indice <=138)
        return " (Tramvai 4) ";
    else if(indice>=139 && indice <=179)
        return " (Tramvai 5) ";
    else if(indice>=180 && indice <=210)
        return " (Tramvai 6) ";
    else if(indice>=211 && indice <=251)
        return " (Tramvai 7) ";
    else if(indice>=252 && indice <=285)
        return " (Tramvai 8) ";
    else if(indice>=286 && indice <=305)
        return " (Tramvai 9) ";
    else if(indice>=306 && indice <=330)
        return " (Tramvai 10) ";
    else if(indice>=331 && indice <=365)
        return " (Firobuz 11) ";
    else if(indice>=366 && indice <=380)
        return " (Firobuz 13) ";
    else if(indice>=381 && indice <=405)
        return " (Firobuz 14) ";
    else if(indice>=406 && indice <=417)
        return " (Firobuz 15) ";
    else if(indice>=418 && indice <=430)
        return " (Firobuz 16) ";
    else if(indice>=431 && indice <=452)
        return " (Firobuz 17) ";
    else if(indice>=453 && indice <=471)
        return " (Firobuz 18) ";
    else if(indice>=472 && indice <=486)
        return " (Firobuz 19) ";
    else if(indice>=487 && indice <=498)
        return " (Autobuz 21) ";
    else if(indice>=499 && indice <=517)
        return " (Autobuz 28) ";
    else if(indice>=518 && indice <=541)
        return " (Autobuz 32) ";
    else if(indice>=542 && indice <=555)
        return " (Autobuz 33) ";
    else if(indice>=556 && indice <=568)
        return " (Autobuz 40) ";
    else if(indice>=569 && indice <=574)
        return " (Autobuz 46) ";
    else if(indice>=575 && indice <=598)
        return " (Express 1) ";
    else if(indice>=599 && indice <=613)
        return " (Express 2) ";
    else if(indice>=614 && indice <=623)
        return " (Express 3) ";
    else if(indice>=624 && indice <=631)
        return " (Express 4) ";
    else if(indice>=632 && indice <=649)
        return " (Express 5) ";
    else if(indice>=650 && indice <=656)
        return " (Express 6) ";
    else if(indice>=657 && indice <=679)
        return " (Express 7) ";
    else if(indice>=680 && indice <=705)
        return " (Express 8) ";
    else return "";
    
}
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private org.jdesktop.swingx.JXMapKit jXMapKit1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables
    java.sql.PreparedStatement statement;
    ConnectToDatabase connection = new ConnectToDatabase();
}
